package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.Scene;
import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.service.ISceneStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.cache.vo.QuestionStatisVO;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.UserInfoStatisVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.SceneStatisVO;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.serach.service.*;
import cn.wolfcode.luowowo.serach.template.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by admin on 2019/8/14.
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private IFlightService flightService;

    @Reference
    private IFlightSeivice flightSearchSeivice;

    @Reference
    private ICabininfoService cabininfoService;

    @Reference
    private ICabinInfoService cabinInfoSearchService;

    @Reference
    private IQuestionRedisService questionRedisService;

    @Reference
    private IAskService askService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IAnswerService answerService;

    @Reference
    private IAnswerRedisService answerRedisService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;
    @Reference
    private ISceneService sceneService;
    @Reference
    private ISceneStatisRedisService sceneStatisRedisService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        /*List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            // 攻略 vo 初始化
            if (strategyStatisRedisService.isStrategyStatisVOExsit(detail.getId())) {
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            try {
                BeanUtils.copyProperties(vo, detail);

                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            strategyStatisRedisService.setStrategyStatisVO(vo);
        }

        for (StrategyDetail detail : list) {
            // 攻略主题初始化
            StrategyTemplate t = new StrategyTemplate();
            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());

            *//*List<String> list = strategyTagService.getTagsBySDetailId(detail.getId());
            t.setTags(list);*//*

            Destination dest = destinationService.getCountry(detail.getDest().getId());
            if (dest != null) {
                t.setCountryId(dest.getId());
                t.setCountryName(dest.getName());
            }
            dest = destinationService.getProvince(detail.getDest().getId());

            if (dest != null) {
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }
            strategySearchService.saveOrUpdate(t);
        }

        ///游记----------------------------------------------------------------------------
        List<Travel> ts = travelService.list();

        for (Travel t : ts) {
            TravelTemplate tt = new TravelTemplate();
            tt.setId(t.getId());
            tt.setAuthorId(t.getAuthor().getId());
            tt.setAuthorName(t.getAuthor().getNickname());
            tt.setCoverUrl(t.getCoverUrl());
            tt.setCreateTime(t.getCreateTime());
            tt.setDestId(t.getDest().getId());
            tt.setDestName(t.getDest().getName());
            tt.setReplynum(t.getReplynum());
            tt.setSummary(t.getSummary());
            tt.setTitle(t.getTitle());
            tt.setViewnum(t.getViewnum());
            travelSearchService.saveOrUpdate(tt);
        }

        //用户----------------------------------------------------------------------------
        List<UserInfo> us = userInfoService.list();
        for (UserInfo u : us) {
            UserInfoTemplate tt = new UserInfoTemplate();
            tt.setId(u.getId());
            tt.setDestName(u.getCity());
            tt.setFansnum(11);
            tt.setHeadUrl(u.getHeadImgUrl());
            tt.setInfo(u.getInfo());
            tt.setLevel(u.getLevel());
            tt.setNickname(u.getNickname());
            tt.setReplynum(10);
            tt.setTravelnum(2);
            userInfoSearchService.saveOrUpdate(tt);
        }

        //目的地----------------------------------------------------------------------------
        List<Destination> ds = destinationService.list();

        for (Destination d : ds) {

            DestinationTemplate tt = new DestinationTemplate();

            tt.setId(d.getId());
            tt.setCoverUrl(d.getCoverUrl());
            tt.setName(d.getName());
            tt.setInfo(d.getInfo());
            destinationSearchService.saveOrUpdate(tt);
        }*/

        //舱位信息初始化-------------------------------------------------------------------------
        List<Cabininfo> cabininfos = cabininfoService.listAll();
        List<CabinInfoTemplate> cabinInfoTemplates = new ArrayList<>();
        for (Cabininfo cabininfo : cabininfos) {
            CabinInfoTemplate cabinInfoTemplate = new CabinInfoTemplate();
            try {
                BeanUtils.copyProperties(cabinInfoTemplate,cabininfo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            cabinInfoTemplates.add(cabinInfoTemplate);
        }


//        // 社区问答 vo 初始化 ------------------------------------------------------------
//        List<Ask> asks = askService.listAll();
//        for (Ask ask : asks) {
////            if (questionRedisService.isAskStati(ask.getId())){
////                continue;
////            }
//            QuestionStatisVO vo = new QuestionStatisVO();
//            try {
//                BeanUtils.copyProperties(vo,ask);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//            questionRedisService.setAskRedis(vo);
//        }
//////        // 社区排行User vo 初始化 ------------------------------------------------------------
//        List<UserInfo> userInfos = userInfoService.list();
//        for (UserInfo userInfo : userInfos) {
////            if (userInfoRedisService.isAskStati(userInfo.getId())){
////                continue;
////            }
//            UserInfoStatisVO vo = new UserInfoStatisVO();
//            try {
//                BeanUtils.copyProperties(vo,userInfo);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//            userInfoRedisService.setUserInfoRedis(vo);
//        }
//        userInfoRedisService.setAnswer();
////        // 社区回答点赞,收藏 vo 初始化 ------------------------------------------------------------
//        List<Answer> answers =  answerService.listAll();
//        for (Answer answer : answers) {
////            if (answerRedisService.isAskStati(answer.getId())){
////                continue;
////            }
//            AnswerStatisVO vo = new AnswerStatisVO();
//            try {
//                BeanUtils.copyProperties(vo,answer);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//            answerRedisService.setAnswerRedisVO(vo);
//        }
//
//        System.out.println("結束");
//
//        System.out.println("开始");

       /* System.out.println("开始初始化");
        List<Travel> list = travelService.list();
        for (Travel travel : list) {
            if (travelStatisRedisService.isTravelStatisVOExsit(travel.getId())) {
                continue;
            }
            TravelStatisVO vo = new TravelStatisVO();
            try {
                BeanUtils.copyProperties(vo, travel);

                vo.setDestId(travel.getDest().getId());
                vo.setDestName(travel.getDest().getName());
                vo.setTravelId(travel.getId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            travelStatisRedisService.setTravelStatisVO(vo);
        }

        System.out.println("结束");

        System.out.println("初始化结束");*/
//       System.out.println("开始初始化");
//        List<Scene> list = sceneService.list();
//        SceneStatisVO vo = null;
//        for (Scene scene : list) {
//            /*if(sceneStatisRedisService.isSceneStatisVOExsit(scene.getId())){
//                continue;
//            }*/
//            vo = new SceneStatisVO();
//            try {
//                BeanUtils.copyProperties(vo, scene);
//                vo.setSceneId(scene.getId());
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//            System.out.println(vo.getSceneId());
//            sceneStatisRedisService.setSceneStatisVO(vo);
//        }
//        System.out.println("初始化结束");
    }
}
